﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.VisualBasic;

namespace Bootstrap5.Design
{
    internal class PaddingProvider : SpacingProvider
    {
        public PaddingProvider() : base(Spacing.Padding)
        {
        }
    }

    internal class MarginProvider : SpacingProvider
    {
        public MarginProvider() : base(Spacing.Margin)
        {
        }
    }

    class SpaceDefinition : BreakingPointModel
    {
        public SpaceSide? Side { get; set; }

    }
    internal abstract class SpacingProvider : BreakPointClassDecoratorProvider<GridSize, SpaceDefinition,ISpacingDecorator>, ISpacingDecorator
    {
        private Spacing _spacing;
        private SpaceDefinition _instance = new ();
        private Dictionary<GridSize, List<SpaceDefinition>> _rules = new();

        public SpacingProvider(Spacing spacing)
        {
            _spacing = spacing;
        }

        public ISpacingDecorator FromX => GetSide(SpaceSide.X);

        public ISpacingDecorator FromY => GetSide(SpaceSide.Y);

        public ISpacingDecorator IsAuto => GetKey(GridSize.Auto);

        public ISpacingDecorator Is1 => GetKey(GridSize.Is1);

        public ISpacingDecorator Is2 => GetKey(GridSize.Is2);

        public ISpacingDecorator Is3 => GetKey(GridSize.Is3);

        public ISpacingDecorator Is4 => GetKey(GridSize.Is4);

        public ISpacingDecorator Is5 => GetKey(GridSize.Is5);

        public ISpacingDecorator FromTop => GetSide(SpaceSide.Top);

        public ISpacingDecorator FromBottom => GetSide(SpaceSide.Bottom);

        public ISpacingDecorator FromLeft => GetSide(SpaceSide.Left);

        public ISpacingDecorator FromRight => GetSide(SpaceSide.Right);

        protected override ISpacingDecorator GetKey(GridSize size)
        {
            SetKey(size);
            return this;
        }

         ISpacingDecorator GetSide(SpaceSide side)
        {
            CurrentModel.Side = side;
            SetDirty();
            return this;
        }

        protected override ISpacingDecorator GetBreakPoint(BreakPoint breakPoint)
        {
            SetBreakPoint(breakPoint);
            return this;
        }

        protected override void BuildClass()
        {
            Rules.AddRange(RuleDic.Select(m => GetCssClass(_spacing, m.Key, m.Value)));
        }

        private string GetCssClass(Spacing space, GridSize size, IEnumerable<SpaceDefinition> values)
        {
            return string.Join(" ", values.Select(m => GetCssClass(space, size, m.Side, m.BreakPoint)));
        }

        private string GetCssClass(Spacing space,GridSize size,SpaceSide? side,BreakPoint? breakPoint)
        {
            var builder = new StringBuilder(space.GetEnumCssClass());
            if (side.HasValue)
            {
                builder.Append(side.Value.GetEnumCssClass());
            }
            builder.Append(CssClassHelper.STRIPER);
            builder.Append(GetBreakPointString(breakPoint));
            builder.Append(size.GetEnumCssClass());
            return builder.ToString();
        }
    }
}
